<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 菜品
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','group_id','num','image','status');
	$list=pdo_getslice('jy_dish_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['num']=($item['num']==-1)?'无限':$item['num'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'菜品列表'),
			array('op'=>'edit','title'=>'添加菜品'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'菜品名称','type'=>'string'),
			array('name'=>'group_id','title'=>'菜品类型','type'=>'select','options'=>'groups'),
			array('name'=>'image','title'=>'菜品图片','type'=>'image'),
			array('name'=>'status','title'=>'是否上架','type'=>'switch','set'=>'set'),
			array('name'=>'num','title'=>'菜品数量','type'=>'string'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['groups']=pdo_getall('jy_dish_goods_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_dish_goods',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_dish_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$item['package']=$item['package']?json_decode($item['package'],true):array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回门店列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'菜品名称','name'=>'title','help'=>''),
					array('type'=>'image','title'=>'菜品图片','name'=>'image','help'=>''),
					array('type'=>'select','title'=>'所属分类','name'=>'group_id','help'=>'','options'=>'groups'),
					array('type'=>'string','title'=>'菜品价格','name'=>'price','help'=>''),
					array('type'=>'string','title'=>'菜品原价','name'=>'old_price','help'=>'如果设置原价则显示，不设置则不显示'),
					array('type'=>'string','title'=>'菜品数量','name'=>'num','help'=>'填-1表示不限制'),
					array('type'=>'text','title'=>'菜品描述','name'=>'desc','help'=>''),
					array('type'=>'switch','title'=>'是否上架','name'=>'status','help'=>'设置是否上架'),
				)
			),
			array(
				'title'=>'运营参数',
				'list'=>array(
					array('type'=>'string','title'=>'排序','name'=>'display','help'=>'排序越大越靠前'),
					array('type'=>'switch','title'=>'是否显示销量','name'=>'show_buy','help'=>'设置是否显示销量'),
					array('type'=>'string','title'=>'虚拟当日销量','name'=>'virtual_day','help'=>'显示销量=虚拟销量+实际销量'),
					array('type'=>'string','title'=>'虚拟总销量','name'=>'virtual_all','help'=>'显示销量=虚拟销量+实际销量'),
					array('type'=>'string','title'=>'购买赠送积分','name'=>'credit','help'=>'不设置则不赠送'),
				),
			),
			array(
				'title'=>'菜品规格',
				'list'=>array(
								array(
										'type'=>'array',
										'name'=>'package',
										'add_tip'=>'添加一个规格',
											'list'=>array(
												array('type'=>'string','name'=>'title','title'=>'规格名称'),
												array('type'=>'string','name'=>'price','title'=>'规格价格'),
												
											),
											'details'=>array(
												array('type'=>'string','name'=>'title','title'=>'规格名称','help'=>''),
												array('type'=>'string','name'=>'price','title'=>'规格价格','help'=>'留空则按菜品价格！'),
												
											)
								),
				)
			)
		)
	);
	$return['groups']=pdo_getall('jy_dish_goods_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['htmls']=$htmls;
	set_html('framess',$this->menu());
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'group_id'=>intval($postdata['group_id']),
		'image'=>trim($postdata['image']),
		'price'=>floatval($postdata['price']),
		'old_price'=>floatval($postdata['old_price']),
		'num'=>isset($postdata['num'])?$postdata['num']:-1,
		'virtual_day'=>intval($postdata['virtual_day']),
		'virtual_all'=>intval($postdata['virtual_all']),
		'credit'=>intval($postdata['credit']),
		'desc'=>trim($postdata['desc']),
		'status'=>intval($postdata['status']),
		'show_buy'=>intval($postdata['show_buy']),
		'display'=>intval($postdata['display']),
		'package'=>ijson_encode($postdata['package']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_dish_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		pdo_insert('jy_dish_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_dish_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}